#ifndef __DATA_MAN__
#define __DATA_MAN__

#include "File_Util.hpp"
#include <unordered_map>
namespace RBC
{
#define SEP_MID ":"
#define SEP_END "\n"
	class DataManager
	{
	public:
		DataManager(const std::string& backup_file)
		:_backup_file(backup_file)
		{
			Initload();
		}
		bool Storage()
		{
			std::string body;
			for (auto hash : _table)
			{
				std::string filename = hash.first;
				std::string unique_ID = hash.second;
				body += filename;
				body += SEP_MID;
				body += unique_ID;
				body += SEP_END;
			}
			RBC::FileUtil fu(_backup_file);
			if (fu.SetContent(&body) == false) return false;
			return true;
		}
		bool Initload()
		{
			RBC::FileUtil fu(_backup_file);//ʵ�����־û��洢���ļ�������
			std::string body;
			if (fu.GetContent(&body) == false) return false;
			//���ļ��������õ�body��,�����Ŀ����Ƶ��ļ��洢��ʽ�� �ļ���:Ψһ��ʶ�����ļ���:Ψһ��ʶ��
			size_t pos = 0;
			while ((pos = body.find(SEP_END)) != std::string::npos)
			{
				std::string tem = body.substr(0, pos);
				size_t tempos = tem.find(SEP_MID);
				_table[tem.substr(0, tempos)] = tem.substr(tempos + 1);
				body = body.substr(pos + 1);
			}
			return true;
		}
		bool Insert(std::string& filename,std::string& unique_ID)
		{
			_table.insert({ filename,unique_ID });
			if (Storage() == false) return false;
			return true;
		}
		bool Update(std::string& filename, std::string& unique_ID)
		{
			_table[filename] = unique_ID;
			if (Storage() == false) return false;
			return true;
		}
		/*bool GetOneByFilename(const std::string& filename, std::string* unique_ID)
		{
			if (_table.find(filename) == _table.end()) return false;
			*unique_ID = _table[filename];
			if (Storage() == false) return false;
			return true;
		}*/
		bool GetOneByFilename(const std::string& filename, std::string* unique_ID)
		{
			RBC::FileUtil fu(filename);
			if (_table.find(fu.FileName()) == _table.end()) return false;
			*unique_ID = _table[fu.FileName()];
			return true;
		}
	private:
		std::unordered_map<std::string, std::string> _table;
		std::string _backup_file;//�־û��洢�ļ�·��
	};
}



#endif // !__DATA_MAN__
